"""
A class to exercise the JT core functions.
"""
import itk
import vtk

import connect2
import ImageCompare

import JT

def Test():
    """
    Test image normalization in both ITK and VTK.

    From this test, it seems that only the ITK function works as expected.
    """

    # These should be normalized images
    image1Name = 'C:/Users/bryan/bryan-code/trunk/Tests/Test_ImageOverlayFixed.mhd'
    image2Name = 'C:/Users/bryan/bryan-code/trunk/Tests/Test_ImageOverlayMoving.mhd'
    outPath = 'C:/Users/bryan/bryan-code/trunk/Tests/'

    #
    # VTK Pipeline
    #%############
    reader1 = vtk.vtkMetaImageReader()
    reader2 = vtk.vtkMetaImageReader()

    caster1 = vtk.vtkImageCast()
    caster2 = vtk.vtkImageCast()

    writer = vtk.vtkMetaImageWriter()

    imageMath = vtk.vtkImageMathematics()


    reader1.SetFileName(image1Name)
    reader2.SetFileName(image2Name)
    reader1.Update()
    reader2.Update()

    caster1.SetInput(reader1.GetOutput())
    caster2.SetInput(reader2.GetOutput())

    caster1.SetOutputScalarTypeToUnsignedChar()
    caster2.SetOutputScalarTypeToUnsignedChar()

    imageMath.SetInput1(caster1.GetOutput())
    imageMath.SetInput2(caster2.GetOutput())
    imageMath.SetOperationToSubtract()
    imageMath.Update()


    #
    # ITK Pipeline
    #%############
    vtk2itk = connect2.vtk2itk()
    itk_writer = itk.ImageFileWriter.IUC2.New()

    vtk2itk.SetInput(imageMath.GetOutput())

    SaveName = outPath + 'TestImageDifference_vtkMath.mhd'
    itk_writer.SetInput(vtk2itk.GetOutput())
    itk_writer.SetFileName(SaveName)
    itk_writer.Write()


    #
    #  Display Pipeline
    #%#################
    itk2vtk = connect2.itk2vtk()
    itk_cast = itk.CastImageFilter.IUC2IF2.New()

    itk_cast.SetInput(vtk2itk.GetOutput())
    itk_cast.Update()

    ic = JT.ImageCanvas()
    ic._SetImage(itk_cast.GetOutput())
    ic.Start()



##    #Test for positive match
##    try:
##        #return 0 if they match, 1 otherwise.
##        assert ImageCompare.compare(inputFilename,itk_SaveName)==0
##        print "ITK Test Success!"
##    except:
##        print "ITK Test Failure!"
##    #Test for positive match
##    try:
##        #return 0 if they match, 1 otherwise.
##        assert ImageCompare.compare(inputFilename,SaveName)==0
##        print "VTK Test Success!"
##    except:
##        print "VTK Test Failure!"



if __name__ == "__main__":
    Test()